<template>
  <div v-loading.fullscreen.lock="loading">
    <div class="page-header">
      <p>修改分数</p>
      <div class="page-header-back" @click="$router.back()">退出</div>
    </div>

    <div class="container">
      <div class="container-left">
        <div class="img-box">
          <img
            @mousedown="startDrag"
            @mousemove="onDrag"
            @mouseup="stopDrag"
            @mouseleave="stopDrag"
            :src="imgList[activeIndex]"
            alt=""
            :style="`transformOrigin: center center;transform: translate(${offsetX}px, ${offsetY}px) scale(${
              scale / 100
            })`"
          />
        </div>
        <div class="page-box">
          <div :class="{ active: activeIndex === 0 }" @click="activeIndex = 0">
            1
          </div>
          <div :class="{ active: activeIndex === 1 }" @click="activeIndex = 1">
            2
          </div>
        </div>
        <div class="templatePage">
          <!-- 进度条缩放 -->
          <el-slider
            v-model="scale"
            :min="1"
            :max="100"
            show-input
            style="width: 100%"
          >
          </el-slider>
        </div>
      </div>
      <div class="container-right">
        <div class="table-box">
          <el-table :data="questionList" style="width: 100%" border stripe>
            <el-table-column label="题目" align="center" width="120">
              <template slot-scope="scope">
                {{ scope.row.questionName }}{{ scope.row.questionNumber }}
              </template>
            </el-table-column>
            <el-table-column label="当前打分" align="center" width="80">
              <template slot-scope="scope">
                {{ scoreData[scope.row.questionId] }}
              </template>
            </el-table-column>
            <el-table-column prop="date" label="修改打分" width="100">
              <template slot-scope="scope">
                <el-input v-model="scope.row.score"></el-input>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="btn-box">
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  systemScoreManageQuestionName,
  systemScoreManageList,
  systemScoreManageAdjustmentScore,
} from "@/api/xmgl/cjgl";
export default {
  data() {
    return {
      loading: false,
      questionList: [],
      scoreData: {},
      imgList: [],
      examStudentIds: "",
      activeIndex: 0,
      scale: 50,
      startX: 0,
      startY: 0,
      offsetX: 0,
      offsetY: 0,
    };
  },

  created() {
    this.loading = true;

    systemScoreManageList({
      admissionNo: this.$route.query.admissionNo,
      examId: this.$route.query.examId,
      examSubjectId: this.$route.query.examSubjectId,
      pageNum: 1,
      pageSize: 1,
    })
      .then((res) => {
        if (res.total > 0) {
          this.examStudentIds = res.rows[0].studentId;
          this.imgList = [res.rows[0].frontImgPath, res.rows[0].backImgPath];
          let scoreData = {};
          let examScoreManageQuestionVos =
            res.rows[0].examScoreManageQuestionVos.filter((item) => {
              return item.ratingSwitch == 1;
            });
          if (examScoreManageQuestionVos.length == 0) {
            this.$router.back();
            this.$modal.msgError("当前没有修改此分数的权限");
            return;
          }
          examScoreManageQuestionVos.map((item) => {
            scoreData[item.questionId] = item.score;
          });
          this.scoreData = scoreData;

          systemScoreManageQuestionName({
            examSubjectId: this.$route.query.examSubjectId,
          })
            .then((res) => {
              this.questionList = res.data
                .filter((item) => {
                  return scoreData[item.questionId]!=null;
                })
                .map((item) => {
                  return {
                    ...item,
                    score: "",
                  };
                });
            })
            .finally(() => {
              this.loading = false;
            });
        }
      })
      .catch(() => {
        this.loading = false;
      });
  },
  methods: {
    submit() {
      let questionList = this.questionList.filter((item) => {
        return item.score;
      });
      if (questionList.length == 0) {
        this.$modal.alert("请填写要修改的分数");
        return;
      }
      let data = {
        examId: this.$route.query.examId,
        examSubjectId: this.$route.query.examSubjectId,
        examScoreBasicQuestionInputs: questionList,
        roundOff: "4",
        examStudentIds: [this.examStudentIds],
      };
      this.loading = true;
      systemScoreManageAdjustmentScore(data)
        .then((res) => {
          this.$modal.msgSuccess("修改成功");
          this.$router.back();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 拖拽
    startDrag(e) {
      this.isDragging = true;
      this.startX = e.clientX - this.offsetX;
      this.startY = e.clientY - this.offsetY;
    },
    onDrag(e) {
      if (!this.isDragging) return;
      this.offsetX = e.clientX - this.startX;
      this.offsetY = e.clientY - this.startY;
    },
    stopDrag() {
      this.isDragging = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.page-header {
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #ffffff;
  background: #1a48db;
  font-size: 16px;
  padding: 0 24px 0 44px;

  p {
    margin: 0;
  }

  .page-header-back {
    width: 76px;
    height: 32px;
    background: #ffffff;
    border-radius: 16px;
    line-height: 32px;
    text-align: center;
    color: #1a48db;
    font-size: 16px;
    cursor: pointer;

    &:active {
      opacity: 0.8;
    }
  }
}
.container {
  height: calc(100vh - 50px);
  display: flex;
  .container-left {
    width: calc(100vw - 310px);
    height: 100%;
    position: relative;
    .img-box {
      width: 100%;
      height: 100%;
      overflow: hidden;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        -webkit-user-drag: none;
        user-drag: none;
      }
    }

    .page-box {
      height: 50px;
      position: absolute;
      left: 20px;
      bottom: 0;
      display: flex;
      align-items: center;
      div {
        width: 32px;
        height: 32px;
        line-height: 32px;
        font-size: 14px;
        text-align: center;
        border: 1px solid #dee1e7;
        margin-right: 14px;
        cursor: pointer;
        background: #ffffff;

        &.active {
          color: #ffffff;
          background: #1a48db;
          border-color: #1a48db;
        }
      }
    }

    .templatePage {
      width: 350px;
      position: absolute;
      right: 20px;
      bottom: 20px;
      z-index: 10;
      display: flex;
      align-items: center;
    }
  }
  .container-right {
    width: 310px;
    background: #fff;
    height: 100%;

    .table-box {
      height: calc(100% - 50px);
      overflow-y: auto;
      overflow-x: hidden;
    }
    .btn-box {
      height: 50px;
      padding: 0 20px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      box-shadow: 0 4px 16px 0 #5572921a;
    }
  }
}
</style>